//
//  RankingListViewController.m
//  rrqs
//
//  Created by mac book on 2019/4/29.
//

#import "RankingListViewController.h"
#import "RankingChildViewController.h"
#import "SGPagingView.h"


@interface RankingListViewController ()
<SGPageTitleViewDelegate,
SGPageContentScrollViewDelegate>

@property (nonatomic, strong) SGPageTitleView *pageTitleView;
@property (nonatomic, strong) SGPageContentScrollView *pageContentCollectionView;
@property (weak, nonatomic) IBOutlet UIButton *todayBut;
@property (weak, nonatomic) IBOutlet UIButton *yesterdayBut;
@property (weak, nonatomic) IBOutlet UIButton *monthBut;
@property (nonatomic, strong)  NSArray * butsArr;



@end

@implementation RankingListViewController


- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupPageView:@[@"单量",@"准时率",@"平均时长",@"配送里程"]];
    [_yesterdayBut setTitleColor:KBlue_color forState:UIControlStateNormal];
    [_monthBut setTitleColor:KBlue_color forState:UIControlStateNormal];
}

- (void)setupPageView:(NSArray *)titleArr {
    
    SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
    configure.titleSelectedColor = [UIColor whiteColor];
    configure.indicatorColor     = [UIColor whiteColor];
        configure.titleColor         = KBlue_color;
    _pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, 50, self.view.frame.size.width, 44) delegate:self titleNames:titleArr configure:configure];
    self.pageTitleView.backgroundColor = kgreen_color;
    [self.view addSubview:_pageTitleView];
    //添加子控制器
    NSMutableArray * childArr = [NSMutableArray array];
    for (int i = 0 ; i < titleArr.count; i ++ ) {
        RankingChildViewController *childVC = NewFunctionStoryboard(@"RankingChildViewController");
        childVC.type = [NSString stringWithFormat:@"%d",i + 1];
        [childArr addObject:childVC];
    }
    [UserInfo saveRankinStatus:@"1"];

    CGFloat ContentCollectionViewHeight = kMainScreenHeight - CGRectGetMaxY(_pageTitleView.frame) - 64;
    self.pageContentCollectionView = [[SGPageContentScrollView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_pageTitleView.frame), self.view.frame.size.width, ContentCollectionViewHeight) parentVC:self childVCs:childArr];
    _pageContentCollectionView.delegatePageContentScrollView = self;
    [self.view addSubview:_pageContentCollectionView];
}

- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex {
    [self.pageContentCollectionView setPageContentScrollViewCurrentIndex:selectedIndex];
}

- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex{
    [self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex];
}

-(NSArray *)butsArr{
    if (!_butsArr) {
        _butsArr = @[_todayBut,_yesterdayBut,_monthBut];
    }
    return _butsArr;
}
/**
 

 @param sender tag 0 今天 1 昨天 2 本月
 */
- (IBAction)headButsAction:(UIButton *)sender {
    for (UIButton * but  in self.butsArr) {
        if (but == sender) {
            [but setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }else{
            [but setTitleColor:KBlue_color forState:UIControlStateNormal];
        }
    }
    [UserInfo saveRankinStatus:[NSString stringWithFormat:@"%ld", sender.tag + 1]];

    //更新status
    [[NSNotificationCenter defaultCenter]postNotificationName:@"UpdatStatus" object:[NSString stringWithFormat:@"%ld", sender.tag + 1]];
}





@end
